package com.doradance.model.local;


import android.content.Context;
import android.text.TextUtils;


import com.doradance.model.remote.bean.LoginBean;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 * Description:
 * Created by Andy Hong on 10/19/2017.
 */

public class UserCenter {
    private static LoginBean loginBean;
    private static UserBean userBean;


    public static LoginBean getLoginBean() {
        if (loginBean == null) {
            loginBean = new LoginBean();
            loginBean.setUserId("");
        }
        return loginBean;
    }

    public static boolean isLogin() {
        if (TextUtils.isEmpty(getLoginBean().getAccessToken())) {
            return false;
        } else {
            return true;
        }

    }

    public static UserBean getUserBean() {
        if (userBean == null) {
            userBean = new UserBean();
        }
        return userBean;
    }


    public static String getUserId() {
        return getLoginBean().getUserId();
    }


    public static void initialize(Context context) {
        loginBean = new LoginBeanDao(context).read();
        userBean = new UserBeanDao(context).read();
    }


    /**
     * 保存用户token
     *
     * @return
     */
    public static class LoginBeanDao {
        private Context context;
        private final String fileName = "iizk";

        public LoginBeanDao(Context context) {
            this.context = context;
        }

        public void save(LoginBean bean) {
            FileOutputStream fos = null;
            ObjectOutputStream oos = null;
            try {
                fos = context.openFileOutput(fileName, Context.MODE_PRIVATE);
                oos = new ObjectOutputStream(fos);
                oos.writeObject(bean);
                loginBean = bean;
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (fos != null) {
                    try {
                        fos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (oos != null) {
                    try {
                        oos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }

        public LoginBean read() {
            FileInputStream fis = null;
            ObjectInputStream ois = null;
            try {
                File targetFile = new File(context.getFilesDir(), fileName);
                if (!targetFile.exists()) {
                    return getLoginBean();
                }
                fis = context.openFileInput(fileName);
                ois = new ObjectInputStream(fis);
                return (LoginBean) ois.readObject();
            } catch (Exception e) {
                e.printStackTrace();

            } finally {
                if (fis != null) {
                    try {
                        fis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (ois != null) {
                    try {
                        ois.close();
                    } catch (IOException e) {
                    }
                }
            }
            return null;
        }

        public boolean delete() {
            File targetFile = new File(context.getFilesDir(), fileName);
            UserCenter.loginBean = null;
            if (!targetFile.exists()) {
                return true;
            } else {
                return targetFile.delete();
            }
        }
    }


    /**
     * 保存用户相关数据
     *
     * @return
     */
    public static class UserBeanDao {
        private Context context;
        private final String fileName = "fdff";

        public UserBeanDao(Context context) {
            this.context = context;
        }

        public void save(UserBean bean) {
            FileOutputStream fos = null;
            ObjectOutputStream oos = null;
            try {
                fos = context.openFileOutput(fileName, Context.MODE_PRIVATE);
                oos = new ObjectOutputStream(fos);
                oos.writeObject(bean);
                userBean = bean;
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (fos != null) {
                    try {
                        fos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (oos != null) {
                    try {
                        oos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }

        public boolean delete() {
            File targetFile = new File(context.getFilesDir(), fileName);
            UserCenter.userBean = null;
            if (!targetFile.exists()) {
                return true;
            } else {
                return targetFile.delete();
            }
        }

        public UserBean read() {
            FileInputStream fis = null;
            ObjectInputStream ois = null;
            try {
                File targetFile = new File(context.getFilesDir(), fileName);
                if (!targetFile.exists()) {
                    return getUserBean();
                }
                fis = context.openFileInput(fileName);
                ois = new ObjectInputStream(fis);
                return (UserBean) ois.readObject();
            } catch (Exception e) {
                e.printStackTrace();

            } finally {
                if (fis != null) {
                    try {
                        fis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (ois != null) {
                    try {
                        ois.close();
                    } catch (IOException e) {
                    }
                }
            }
            return null;
        }
    }




}
